using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Emlyn.Common.Data.SqlServer.SqlManagers;
using Emlyn.Common.Data.SqlServer.DataLayer;

namespace Emlyn.Website.Users
{
    public class UserDataLayer: SimpleTableDataLayer<User, Guid>
    {
        public DataSet GetUserByUserName(string aUserName)
        {
            string lSql = "select * from tblEOUser where UserName=@UserName";
            SqlParameter lUserNameParameter = new SqlParameter("@UserName", aUserName);
            List<SqlParameter> lParameters = new List<SqlParameter>();
            lParameters.Add(lUserNameParameter);

            return SqlManager.GetDataSet(lSql, lParameters);
        }

        public DataSet GetUsersByWebsiteCode(string aWebsiteCode)
        {
            string lSql =
                "select u.* " +
                "from tblEOUser u " +
                "where u.WebsiteCode=@WebsiteCode ";
            SqlParameter lWebsiteCodeParameter = new SqlParameter("@WebsiteCode", aWebsiteCode);
            List<SqlParameter> lParameters = new List<SqlParameter>();
            lParameters.Add(lWebsiteCodeParameter);

            return SqlManager.GetDataSet(lSql, lParameters);
        }

        protected override string TableName()
        {
            return "tblEOUser";
        }

        protected override string KeyName()
        {
            return "UserID";
        }

        protected override IList<string> NonKeyAttributeNamesWritable()
        {
            List<string> retval = new List<string>();

            retval.Add("UserName");
            retval.Add("Password");
            retval.Add("AuthLevel");
            retval.Add("Email");
            retval.Add("ValidationCode");
            retval.Add("WebsiteCode");

            return retval;
        }

        protected override IList<string> NonKeyAttributeNamesReadonly()
        {
            List<string> retval = new List<string>();

            return retval;
        }

        protected override Guid DoGetKey(User aInstance)
        {
            return aInstance.UserID;
        }

        protected override object GetParameterValue(User aInstance, string aParameterName)
        {
            switch (aParameterName)
            {
                case "UserName": return aInstance.UserName;
                case "Password": return aInstance.HashedPassword;
                case "AuthLevel": return Convert.ToInt32(aInstance.AuthLevel);
                case "Email": return aInstance.Email;
                case "ValidationCode": return aInstance.ValidationCode;
                case "WebsiteCode": return aInstance.WebsiteCode;
            }

            throw new ApplicationException("Unknown Parameter Requested: " + aParameterName);
        }
    }
}
